/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.sinosoft.rabbit.controller;

import com.alibaba.fastjson.JSONObject;
import javax.servlet.http.HttpServletRequest;
import org.springframework.amqp.core.AmqpAdmin;
import org.springframework.amqp.core.Binding;
import org.springframework.amqp.core.DirectExchange;
import org.springframework.amqp.core.Queue;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

/**
 *
 * @author root
 */
@RestController
@RefreshScope
public class RabbitBuildController {

    @Autowired
    AmqpAdmin a;

    @RequestMapping(value = "/rabbitbuild", method = {RequestMethod.POST, RequestMethod.GET})
    public boolean rabbitbuild(@RequestBody JSONObject param, HttpServletRequest request) {
        try {
            
            String exchange = param.getString("exchange");
            String routingkey = param.getString("routingkey");
            String queue = routingkey;
            
            //a.declareExchange(new DirectExchange(exchange, true, false));
            a.declareQueue(new Queue(queue, true));
            a.declareBinding(new Binding(queue, Binding.DestinationType.QUEUE, exchange, routingkey, null));
            
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
        return true;
    }
}
